home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFtoSpr / c / SavePlanets < prev    next >
Encoding:
Text File  |  2003-11-06  |  7.3 KB  |  216 lines

  1. /*
  2.  *  SFtoSpr - Star Fighter 3000 graphics converter
  3.  *  Savebox for SF3000 Planets file
  4.  *  Copyright (C) 2000  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26.  
  27. /* RISC OS library files */
  28. #include "wimp.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "wimplib.h"
  32. #include "saveas.h"
  33. #include "window.h"
  34. #include "gadgets.h"
  35. #include "flex.h"
  36.  
  37. /* My library files */
  38. #include "err.h"
  39. #include "msgtrans.h"
  40. #include "hourglass.h"
  41. #include "ViewsMenu.h"
  42. #include "Macros.h"
  43. #include "TboxBugs.h"
  44. #include "FilePerc.h"
  45.  
  46. /* Local headers */
  47. #include "SFformats.h"
  48. #include "Utils.h"
  49. #include "Loader.h"
  50. #include "Main.h"
  51. #include "SavePlanets.h"
  52.  
  53. typedef struct _SavePlanetsData
  54. {
  55.   ObjectId          saveas_id;
  56.   ObjectId          window_id;
  57.   char             *reset_file_name;
  58.   SF_PlanetsSetHdr *planets_data; /* flex block */
  59. } SavePlanetsData;
  60.  
  61. /* -----------------------------------------------------------------------
  62.  *                       Function prototypes
  63.  */
  64.  
  65. static ToolboxEventHandler _SavePlanets_savehandler, _SavePlanets_buttonshandler, _SavePlanets_deletedhandler;
  66.  
  67. /* -----------------------------------------------------------------------
  68.  *                         Public functions
  69.  */
  70.  
  71. ObjectId SavePlanets_create(char *savepath, int data_saved, SF_PlanetsSetHdr **planets_data)
  72. {
  73.   /* Must either RE-ANCHOR planets_data or flex_free() it
  74.      (will only be flex_free'd if we return NULL_ObjectId) */
  75.   SavePlanetsData *newblk;
  76.   char *views_path;
  77.  
  78.   /* Grab memory */
  79.   newblk = malloc(sizeof(SavePlanetsData));
  80.   if(newblk == NULL)
  81.     MG_RETV("NoMem", NULL_ObjectId) /* failure */
  82.   newblk->reset_file_name = NULL;
  83.  
  84.   /* Create object */
  85.   if(E(toolbox_create_object(0, "SprToPla", &newblk->saveas_id)))
  86.     goto errexit_free;
  87.   if(E(saveas_get_window_id(0, newblk->saveas_id, &newblk->window_id)))
  88.     goto errexit_delete;
  89.  
  90.   /* Store default settings */
  91.   if((newblk->reset_file_name = copystring(savepath)) == NULL)
  92.     goto errexit_delete;
  93.  
  94.   /* Add entry to iconbar menu */
  95.   if(data_saved)
  96.     views_path = savepath; /* we have real filepath */
  97.   else
  98.     views_path = ""; /* dummy filepath */
  99.   if(E(ViewsMenu_add(newblk->saveas_id, msgs_lookup_sub1("SprPlaList", tail(savepath, 3)), views_path)))
  100.     goto errexit_delete;
  101.  
  102.   /* Set up window */
  103.   if(E(saveas_set_file_name(0, newblk->saveas_id, savepath))
  104.   || E(saveas_set_file_type(0, newblk->saveas_id, FILETYPE_PLANETS))
  105.   || E(saveas_set_file_size(0, newblk->saveas_id, flex_size((flex_ptr)planets_data))))
  106.     goto errexit_menuremove;
  107.  
  108.   /*
  109.      Note the ObjectDeleted handler is registered AFTER anything that could cause an error and therefore premature deletion!
  110.   */
  111.   if(E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_SaveToFile,  _SavePlanets_savehandler, newblk))
  112.   || E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_DialogueCompleted, delete_object_handler, newblk))
  113.   || E(event_register_toolbox_handler(newblk->window_id, ActionButton_Selected, _SavePlanets_buttonshandler, newblk))
  114.   || E(event_register_toolbox_handler(newblk->saveas_id, Toolbox_ObjectDeleted,  _SavePlanets_deletedhandler, newblk)))
  115.     goto errexit_menuremove;
  116.  
  117.   flex_reanchor((flex_ptr)&newblk->planets_data, (flex_ptr)planets_data);
  118.   return newblk->saveas_id; /* success */
  119.  
  120.   errexit_menuremove:
  121.     RE(ViewsMenu_remove(newblk->saveas_id))
  122.   errexit_delete:
  123.     RE(toolbox_delete_object(0, newblk->saveas_id))
  124.   errexit_free:
  125.     free(newblk->reset_file_name);
  126.     free(newblk);
  127.     return NULL_ObjectId; /* failure */
  128. }
  129.  
  130. /* -----------------------------------------------------------------------
  131.  *                          Private functions
  132.  */
  133.  
  134. /*
  135.  * Toolbox event handlers
  136.  */
  137.  
  138. static int _SavePlanets_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  139. {
  140.   SaveAsSaveToFileEvent *save_to_file_block = (SaveAsSaveToFileEvent *)event;
  141.   SavePlanetsData *blk = (SavePlanetsData *)handle;
  142.  
  143.   /* Save to compressed Fednet file */
  144.   _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, save_to_file_block->filename, FILETYPE_PLANETS, (flex_ptr)&blk->planets_data);
  145.   if(err != NULL)
  146.     err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
  147.  
  148.   saveas_file_save_completed(err == NULL, id_block->self_id, save_to_file_block->filename);
  149.   return 1; /* claim event */
  150. }
  151.  
  152. /* ----------------------------------------------------------------------- */
  153.  
  154. static int _SavePlanets_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  155. {
  156.   /* ActionButton clicked on underlying window */
  157.   ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event;
  158.   SavePlanetsData *blk = (SavePlanetsData *)handle;
  159.  
  160.   if(!FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust))
  161.     return 0; /* not interested */
  162.   
  163.   switch(id_block->self_component) {
  164.     case 0x82bc02: /* Cancel button */
  165.       /* Reset dbox state */
  166.       if(blk->reset_file_name != NULL)
  167.         RE(saveas_set_file_name(0, blk->saveas_id, blk->reset_file_name))
  168.       break;
  169.       
  170. #ifndef SAVEAS_CRAP /* no point saving state if dbox closes anyway */
  171.     case 0x82bc03: /* Save button */
  172.       /* Record dbox state */
  173.       int len;
  174.       E_RETV(saveas_get_file_name(0, blk->saveas_id, NULL, 0, &len), 1)
  175.       free(blk->reset_file_name);
  176.       blk->reset_file_name = malloc(len+1);
  177.       if(blk->reset_file_name == NULL)
  178.         err_complain(255, msgs_global("NoMem"));
  179.       else
  180.         RE(saveas_get_file_name(0, blk->saveas_id, blk->reset_file_name, len, NULL))
  181.       break;
  182. #endif
  183.  
  184.     default:
  185.       return 0; /* unknown component */
  186.   }
  187.  
  188.   return 1; /* claim event */
  189. }
  190.  
  191. /* ----------------------------------------------------------------------- */
  192.  
  193. static int _SavePlanets_deletedhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  194. {
  195.   SavePlanetsData *blk = (SavePlanetsData *)handle;
  196.  
  197.   /* Deregister handlers */
  198.   RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_SaveToFile, _SavePlanets_savehandler, handle))
  199.   RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_DialogueCompleted, delete_object_handler, handle))
  200.   RE(event_deregister_toolbox_handler(blk->window_id, ActionButton_Selected, _SavePlanets_buttonshandler, handle))
  201.   RE(event_deregister_toolbox_handler(id_block->self_id, Toolbox_ObjectDeleted, _SavePlanets_deletedhandler, handle))
  202.  
  203.   /* Remove from iconbar menu */
  204.   RE(ViewsMenu_remove(id_block->self_id))
  205.  
  206.   /* free memory */
  207.   flex_free((flex_ptr)&blk->planets_data);
  208.   free(blk->reset_file_name);
  209.   free(blk);
  210.  
  211.   if(last_savebox == id_block->self_id)
  212.     last_savebox = NULL_ObjectId;
  213.  
  214.   return 1; /* claim event */
  215. }
  216.